package com.wing.product.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.utils.CodeUtil;
import com.wing.common.utils.JsonResult;
import com.wing.product.dao.ProductPackLabelDetailDao;
import com.wing.product.model.entity.ProductPackLabel;
import com.wing.product.model.entity.ProductPackLabelDetail;
import com.wing.product.model.request.ProductPackLabelDetailForm;
import com.wing.product.model.response.ProductPackLabelDetailVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 产品包装条码-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class ProductPackLabelDetailServiceImpl extends ServiceImpl<ProductPackLabelDetailDao, ProductPackLabelDetail> implements ProductPackLabelDetailService {
    @Autowired
    private ProductPackLabelService productPackLabelService;

    @Override
    public int add(ProductPackLabelDetailForm productPackLabelDetailForm) {

        //校验

        ProductPackLabelDetail productPackLabelDetail =new ProductPackLabelDetail();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(ProductPackLabelDetail.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(productPackLabelDetailForm, productPackLabelDetail, copyOptions);

        return baseMapper.insert(productPackLabelDetail);
    }

    @Override
    public List<ProductPackLabelDetailVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<ProductPackLabelDetailVO> pageList4app(Page page, Map<String, Object> params) {
        List<ProductPackLabelDetailVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public ProductPackLabelDetailVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    public List<ProductPackLabelDetailVO> getPackLabelDetaiListByJobId(Long jobId) {
        return baseMapper.getPackLabelDetaiListByJobId(jobId);
    }

    @Override
    public Map<String,Object> savePackLabelInfo(ProductPackLabelDetailForm productPackLabelDetailForm) {
        Map<String,Object> newMap = new HashMap<>();
        //获取标签条码明细信息
        QueryWrapper<ProductPackLabel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("code", productPackLabelDetailForm.getPackLabelCode());
        ProductPackLabel one = productPackLabelService.getOne(queryWrapper);
        if (Objects.isNull(one)) {
           newMap.put("one",one);
        }
        QueryWrapper<ProductPackLabelDetail> params = new QueryWrapper<>();
        params.eq("pack_label_code", productPackLabelDetailForm.getPackLabelCode());
        String code = CodeUtil.getCode(this.count(params));
        productPackLabelDetailForm.setCode(productPackLabelDetailForm.getPackLabelCode().concat(code));
        newMap.put("addCount",this.add(productPackLabelDetailForm));
        return newMap;
    }

}
